package com.aqarat.managedBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Apartment;
import com.aqarat.entity.Building;
import com.aqarat.entity.Floor;
import com.common.Constant;
import com.core.entity.Lookup;
import com.core.entity.LookupType;
import com.core.managedBean.BaseBean;
import com.core.util.Utils;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class ApartmentSearchBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private Apartment apartment;
	private AqaratController aqaratController;
	private List<Building> buildingList;
	private List<Floor> floorList;
	private List<Lookup> reservationStatusList;
	private List<Lookup> constructStatusList;
	private List<Lookup> taxesStatusList;
	private Building building;
	private Floor floor;

	// ===========//
	public List<Long> maxFloors;
	public List<Long> maxApartments;
	// ===========//
	LazyDataModel<Apartment> lazyLoad = new LazyDataModel<Apartment>() {
		@Override
		public List<Apartment> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
			floor.setBuilding(building);
			apartment.setFloor(floor);
			lazyLoad.setRowCount(aqaratController.getApartmentListRowCount(apartment).intValue());
			return aqaratController.getApartmentList(first, pageSize, apartment);
		}
	};

	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		if (apartment == null) {
			apartment = new Apartment();
		}
		if (building == null) {
			building = new Building();
		}
		if (floor == null) {
			floor = new Floor();
		}

		loadBuildingList();
		loadMaxFloorsAndApartments();
		loadAlllist();
	}

	public void loadAlllist() {
		try {
			this.reservationStatusList = aqaratController.getLookupList(LookupType.APARTMENT_RESERVESTATUS.getRecid());
			this.constructStatusList = aqaratController.getLookupList(LookupType.APARTMENT_CONSTRUCTSTATUS.getRecid());
			this.taxesStatusList = aqaratController.getLookupList(LookupType.APARTMENT_TAXESSSTATUS.getRecid());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void loadMaxFloorsAndApartments() {
		if (Utils.isNull(floorList)) {
			floorList = new ArrayList<Floor>();
		} else {
			floorList.clear();
		}
		for (Long m = 1L; m <= Constant.MAX_FLOORS; m++) {
			Floor f = new Floor();
			f.setRecid(null);
			f.setNo(m);
			floorList.add(f);
		}
	}

	public void loadBuildingList() {
		try {
			buildingList = aqaratController.getBuildingList(null);
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}

	}

	public void loadFloorList() {
		try {
			if (Utils.isNotEmpty(floorList)) {
				floorList.clear();
			}
			if (Utils.isNotEmpty(building.getRecid())) {
				floorList = aqaratController.getFloorListByBuildingId(building.getRecid());

			} else {
				loadMaxFloorsAndApartments();
			}
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}
	}

	public void searchApartmentList() {
		try {
			// apartment = new Apartment();

			getApartmentList();
			// apartmentList = aqaratController.getApartmentList(apartment);
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}
	}

	public void saveApartment() {
		try {
			aqaratController.saveApartment(apartment);
			super.addInfoMessage("Successfully Saved");
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured when Saving");
		}
	}

	public String toSaleApartment() {
		super.setFlashAttribute("apartment", apartment);
		return "toSaleApartment";
	}
	public String toApartmentDetails() {
		super.setFlashAttribute("apartment", apartment);
		return "toApartmentDetails";
	}

	public String toCustomerPayments() {
		super.setFlashAttribute("customer", apartment.getCustomer());
		return "toCustomerPayments";
	}

	// ========================//
	public LazyDataModel<Apartment> getApartmentList() {
		return lazyLoad;
	}

	public Apartment getApartment() {
		return apartment;
	}

	public void setApartment(Apartment apartment) {
		this.apartment = apartment;
	}

	public AqaratController getAqaratController() {
		return aqaratController;
	}

	public void setAqaratController(AqaratController aqaratController) {
		this.aqaratController = aqaratController;
	}

	public void setBuildingList(List<Building> buildingList) {
		this.buildingList = buildingList;
	}

	public List<Building> getBuildingList() {
		return buildingList;
	}

	public void setFloorList(List<Floor> floorList) {
		this.floorList = floorList;
	}

	public List<Floor> getFloorList() {
		return floorList;
	}

	public void setBuilding(Building building) {
		this.building = building;
	}

	public Building getBuilding() {
		return building;
	}

	public void setFloor(Floor floor) {
		this.floor = floor;
	}

	public Floor getFloor() {
		return floor;
	}

	// public void setApartmentList(List<Apartment> apartmentList) {
	// this.apartmentList = apartmentList;
	// }
	//
	// public List<Apartment> getApartmentList() {
	// return apartmentList;
	// }

	public final List<Lookup> getReservationStatusList() {
		return reservationStatusList;
	}

	public final void setReservationStatusList(List<Lookup> reservationStatusList) {
		this.reservationStatusList = reservationStatusList;
	}

	public final List<Lookup> getConstructStatusList() {
		return constructStatusList;
	}

	public final void setConstructStatusList(List<Lookup> constructStatusList) {
		this.constructStatusList = constructStatusList;
	}

	public final List<Lookup> getTaxesStatusList() {
		return taxesStatusList;
	}

	public final void setTaxesStatusList(List<Lookup> taxesStatusList) {
		this.taxesStatusList = taxesStatusList;
	}

	public final List<Long> getMaxFloors() {
		return maxFloors;
	}

	public final void setMaxFloors(List<Long> maxFloors) {
		this.maxFloors = maxFloors;
	}

	public final List<Long> getMaxApartments() {
		return maxApartments;
	}

	public final void setMaxApartments(List<Long> maxApartments) {
		this.maxApartments = maxApartments;
	}
}
